local skel = fk.CreateSkill {
  name = "efengqi__kuangfeng",
}

Fk:loadTranslationTable{
  ["efengqi__kuangfeng"] = "狂风",
  [":efengqi__kuangfeng"] = "出牌阶段限一次，你可以从弃牌堆获得一张“星”，令一名角色受到火焰伤害+1，直到你下回合开始。",

  ["@@efengqi__kuangfeng"] = "狂风",
  ["#efengqi__kuangfeng"] = "狂风:弃牌堆获得一张“星”，令一名角色受到火焰伤害+1，直到你下回合开始",

  ["$kuangfeng1"] = "风~~起~~",
  ["$kuangfeng2"] = "万事俱备，只欠业火。",
}

skel:addEffect("active", {
  prompt = "#efengqi__kuangfeng",
  card_num = 1,
  target_num = 1,
  expand_pile = function ()
    return table.filter(Fk:currentRoom().discard_pile, function (id)
      return Fk:getCardById(id):getMark("@@efengqi__star") ~= 0
    end)
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Card.DiscardPile
    and Fk:getCardById(to_select):getMark("@@efengqi__star") ~= 0
  end,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function (self, room, use)
    local player, to = use.from, use.tos[1]
    room:obtainCard(player, use.cards, true, fk.ReasonPrey, player, skel.name)
    if not to.dead and not player.dead then
      room:addTableMarkIfNeed(to, "@@efengqi__kuangfeng", player.id)
    end
  end,
})

skel:addEffect(fk.DamageInflicted, {
  is_delay_effect = true,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return target == player and data.damageType == fk.FireDamage and #target:getTableMark("@@efengqi__kuangfeng") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data:changeDamage(#target:getTableMark("@@efengqi__kuangfeng"))
  end,
})

---@param player ServerPlayer
local function clearMark(player)
  local room = player.room
  for _, target in ipairs(room.alive_players) do
    local mark = target:getTableMark("@@efengqi__kuangfeng")
    local new = table.filter(mark, function(id) return id ~= player.id end)
    if #new < #mark then
      room:setPlayerMark(target, "@@efengqi__kuangfeng", #new > 0 and new or 0)
    end
  end
end

skel:addLoseEffect(function (self, player, is_death)
  clearMark(player)
end)

skel:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    clearMark(player)
  end,
})

return skel
